﻿package com.xuanque.mahjongDice.display {
	
	import datas.Player;
	
	import com.xuanque.mahjongDice.ScrollBarui.*;
	import flash.display.*;
	import flash.events.*;
	import utils.transitions.*;
	
	public class PlayerList extends Sprite{
		
		private var contentList:Sprite;
		private var list_length:uint;
		private var _width:Number;
		private var _height:Number;
		
		public function PlayerList(){
			
			//init();
		}
		
		public function init():void {
			
			contentList = new Sprite();
			this.content_mc.x = 0;
			this.content_mc.addChild(this.contentList);
			this.content_mc.mask = mask_mc;
			
			new ScrollBoxDealWith(this.content_mc, this.mask_mc, this.scrollbarX, this.scrollbarY, stage);
		}
		
		
		
		
		
		/*
		 * @列表添加：addPlayerUser(_name:String,prev_grade:String,all_grade:String)
		 * 
		*/
		public function addPlayerUser(player:Player):void {
			
			var item:PlayerItem = new PlayerItem(player);
			
			list_length = this.contentList.numChildren;
			if (list_length > 0) {
				var newItem:PlayerItem = contentList.getChildAt(list_length - 1) as PlayerItem;
				item.y = newItem.y + 22;
			}else {
				item.y = content_mc.playerListTitle.height + 2;
			}
			
			this.contentList.addChild(item);
			
			this.refresh();
		}
		
		
		/*
		 * 
		 * @移除某一用户：removeUser(player:Player)
		*/
		public function removeUser(player:Player):void {
			
			var name:String = player.getLoginName();
			
			this.removeItemAt(name);
			
		}
		
		
		/*
		 * 
		 * @移除列：removeItemAt(_text:String)
		*/
		public function removeItemAt(_name:String) {
			var count = contentList.numChildren;
			for (var i:int = 0; i <count; i++) {
				var item:PlayerItem = contentList.getChildAt(i) as PlayerItem;
				if (item._text == _name) {
					contentList.removeChild(item);
					//trace("removeItemAt  count = " + count);
					this.refresh();
					return;
				}
				
			}
			
		}
		
		/*
		 * 更新设置用户信息 
		*/
		public function refreshText(player:Player):void {
			
			var _name:String = player.getLoginName();
			
			var count = contentList.numChildren;
			for (var i:int = 0; i <count; i++) {
				var item:PlayerItem = contentList.getChildAt(i) as PlayerItem;
				if (item._text == _name) {
					
					item.setText(player);
					return;
				}
				
			}
			
			
		}
		
		
		private function refresh(){
			
			var count = contentList.numChildren;
			var newY:uint = 0;
			for (var i:int = 0; i <count; i++) {
				var item:PlayerItem = contentList.getChildAt(i) as PlayerItem;
				Tweener.addTween(item,{y:newY +22,time:0.5});
				//newY = item.y;
				newY +=20;
			}
			
			//trace("refresh  count = " + count);
		}
		
		
		
	}
}